<?php
// +----------------------------------------------------------------------
// | Yibu Cabinet [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.yibuxinxi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LGW < 1913630280@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use dragon\Config\BaseConfigModel;
use dc\controller\AdminBaseController;
use think\Config;

class BaseConfigController extends AdminBaseController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    // 基本配置列表
    public function index()
    {
        $startTime = date('Y-m-d H:i:s', Config::get('e_start_time'));
        $endTime = date('Y-m-d H:i:s', Config::get('e_end_time'));
        $baseConfigModel = new BaseConfigModel();
        $list = $baseConfigModel->select()
            ->toArray();
        foreach ($list as $key => $val) {
            $val['key'] == 'e_start_time' and $list[$key]['val'] = $startTime;
            $val['key'] == 'e_end_time' and $list[$key]['val'] = $endTime;
        }
        $this->assign('list', $list);
        return $this->fetch('base_config/index');
    }

    //添加
    public function add()
    {
        return $this->fetch('base_config/add');
    }

    //添加提交
    public function addPost()
    {
        $data = $this->request->post();
        $BaseConfigModel = new BaseConfigModel();
        $BaseConfigModel->save($data);
        $this->success("添加成功！", url("BaseConfig/add"));
    }


    // 编辑提交
    public function editPost()
    {
        $id = $this->request->param('id', 0, 'intval');
        $baseConfig = BaseConfigModel::get($id);
        $data = $this->request->post();
        if ($baseConfig['key'] == 'e_start_time' || $baseConfig['key'] == 'e_end_time') {
            $data['val'] = strtotime($data['val']);
        }
        $where['id'] = $id;
        $BaseConfigModel = new BaseConfigModel();
        $BaseConfigModel->save($data, $where);
        $this->success("保存成功!", "BaseConfig/index");

    }

    // 删除
    public function delete()
    {
        $id = $this->request->param('id', '0', 'intval');
        BaseConfigModel::destroy($id);
        $this->success("删除成功", "BaseConfig/index");
    }

    // 更新缓存
    public function updateConfig()
    {
        BaseConfigModel::setConfig();
        $this->success("更新缓存成功！", '');
    }

}
